package com.itheima.demo05proxy2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyUtil {

    // 创建通用代理对象
    public static <T> T createProxy(T userService) {
        T userServiceProxy = (T) Proxy.newProxyInstance(
                userService.getClass().getClassLoader(),
                userService.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        long start = System.currentTimeMillis();
                        Object result = method.invoke(userService, args);
                        long end = System.currentTimeMillis();
                        System.out.println("耗时：" + (end - start) + "毫秒");
                        return result;
                    }
                }
        );
        return userServiceProxy;
    }
}
